package com.tebiecloud.order.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.order.client.model.entity.Order;
import com.tebiecloud.order.server.service.OrderService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * 订单 前端控制器
 * @author tebie
 * @date 2019-09-17
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<Order>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        Order query = pageParams.mapToObject(Order.class);
        QueryWrapper<Order> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(orderService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<Order> get(@RequestParam("id") Long id) {
        Order entity = orderService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param number 订单号,
     * @param userId 顾客标识id,
     * @param phone 联系电话,
     * @param address 收货地址（不包含实物时不需要收货地址）,
     * @param receiver 收货人,
     * @param status 订单状态，0已取消1待付款2待收货3已发货4已收货(已核销)5退款中6已退款,
     * @param price 订单总费用（单位：分）,
     * @param invoiceType 发票类型，0无需开发票1个人2单位,
     * @param unitName 单位名称,
     * @param recognitionCode 纳税人识别号,
     * @param actId 订单关联的活动id,
     * @param actName 活动名称,
     * @param actPic 活动图片,
     * @param actType 活动类型：1爆品活动，2礼包活动,
     * @param payBalances 余额支付，单位分,
     * @param payMgg 美乖乖支付，单位分,
     * @param payReady 实际支付，单位分,
     * @param shutTime 超时支付，关闭订单时间
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "number", required = false) String number,
        @RequestParam(value = "userId", required = false) Long userId,
        @RequestParam(value = "phone", required = false) String phone,
        @RequestParam(value = "address", required = false) String address,
        @RequestParam(value = "receiver", required = false) String receiver,
        @RequestParam(value = "status", required = false) Integer status,
        @RequestParam(value = "price", required = false) Integer price,
        @RequestParam(value = "invoiceType", required = false) Integer invoiceType,
        @RequestParam(value = "unitName", required = false) String unitName,
        @RequestParam(value = "recognitionCode", required = false) String recognitionCode,
        @RequestParam(value = "actId", required = false) Long actId,
        @RequestParam(value = "actName", required = false) String actName,
        @RequestParam(value = "actPic", required = false) String actPic,
        @RequestParam(value = "actType", required = false) Integer actType,
        @RequestParam(value = "payBalances", required = false) Integer payBalances,
        @RequestParam(value = "payMgg", required = false) Integer payMgg,
        @RequestParam(value = "payReady", required = false) Integer payReady,
        @RequestParam(value = "shutTime", required = false) Date shutTime
    ) {
        Order entity = new Order();
        entity.setNumber(number);
        entity.setUserId(userId);
        entity.setPhone(phone);
        entity.setAddress(address);
        entity.setReceiver(receiver);
        entity.setStatus(status);
        entity.setPrice(price);
        entity.setInvoiceType(invoiceType);
        entity.setUnitName(unitName);
        entity.setRecognitionCode(recognitionCode);
        entity.setActId(actId);
        entity.setActName(actName);
        entity.setActPic(actPic);
        entity.setActType(actType);
        entity.setPayBalances(payBalances);
        entity.setPayMgg(payMgg);
        entity.setPayReady(payReady);
        entity.setShutTime(shutTime);
        orderService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param number 订单号
     * @param userId 顾客标识id
     * @param phone 联系电话
     * @param address 收货地址（不包含实物时不需要收货地址）
     * @param receiver 收货人
     * @param status 订单状态，0已取消1待付款2待收货3已发货4已收货(已核销)5退款中6已退款
     * @param price 订单总费用（单位：分）
     * @param invoiceType 发票类型，0无需开发票1个人2单位
     * @param unitName 单位名称
     * @param recognitionCode 纳税人识别号
     * @param actId 订单关联的活动id
     * @param actName 活动名称
     * @param actPic 活动图片
     * @param actType 活动类型：1爆品活动，2礼包活动
     * @param payBalances 余额支付，单位分
     * @param payMgg 美乖乖支付，单位分
     * @param payReady 实际支付，单位分
     * @param shutTime 超时支付，关闭订单时间
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "orderId", required = false) Long orderId,
            @RequestParam(value = "number", required = false) String number,
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "address", required = false) String address,
            @RequestParam(value = "receiver", required = false) String receiver,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "price", required = false) Integer price,
            @RequestParam(value = "invoiceType", required = false) Integer invoiceType,
            @RequestParam(value = "unitName", required = false) String unitName,
            @RequestParam(value = "recognitionCode", required = false) String recognitionCode,
            @RequestParam(value = "actId", required = false) Long actId,
            @RequestParam(value = "actName", required = false) String actName,
            @RequestParam(value = "actPic", required = false) String actPic,
            @RequestParam(value = "actType", required = false) Integer actType,
            @RequestParam(value = "payBalances", required = false) Integer payBalances,
            @RequestParam(value = "payMgg", required = false) Integer payMgg,
            @RequestParam(value = "payReady", required = false) Integer payReady,
            @RequestParam(value = "shutTime", required = false) Date shutTime
    ) {
        Order entity = new Order();
        entity.setOrderId(orderId);
        entity.setNumber(number);
        entity.setUserId(userId);
        entity.setPhone(phone);
        entity.setAddress(address);
        entity.setReceiver(receiver);
        entity.setStatus(status);
        entity.setPrice(price);
        entity.setInvoiceType(invoiceType);
        entity.setUnitName(unitName);
        entity.setRecognitionCode(recognitionCode);
        entity.setActId(actId);
        entity.setActName(actName);
        entity.setActPic(actPic);
        entity.setActType(actType);
        entity.setPayBalances(payBalances);
        entity.setPayMgg(payMgg);
        entity.setPayReady(payReady);
        entity.setShutTime(shutTime);
        orderService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            orderService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            orderService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
